﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Globalization;

namespace LostLib.Programs.Packages
{
	/// <summary>
	/// Represents a package dependency.
	/// </summary>
	[Serializable]
	public class PackageDependency
	{
		/// <summary>
		/// Constructs PackageDependency object.
		/// </summary>
		/// <param name="package">Required package name.</param>
		/// <param name="version">Required package version.</param>
		public PackageDependency(string package, DateTime version)
		{
			Package = package;
			Version = version;
		}

		/// <summary>
		/// Required package name.
		/// </summary>
		public string Package { get; private set; }
		/// <summary>
		/// Required package version.
		/// </summary>
		public DateTime Version { get; private set; }

		//static readonly Regex regexDependency = new Regex(
		//    @"^\s*(?<name>[a-zA-Z\-_0-9]+)\s*\[\s*(?<version>[0-9_\s\.\\\/\-]*)\s*\](?<rest>.*)$",
		//    RegexOptions.Compiled | RegexOptions.Singleline);

		//static PackageDependency Parse(ref string dependancies)
		//{
		//    var match = regexDependency.Match(dependancies);

		//    if (!match.Success) throw new FormatException();

		//    var package = match.Groups["name"].Value;
		//    var version = DateTime.Parse(match.Groups["version"].Value);
		//    dependancies = match.Groups["rest"].Value.Trim();
		//    return new PackageDependency(package, version);
		//}

		/// <summary>
		/// Converts PackageDependency to its string representation.
		/// </summary>
		/// <returns>String representation.</returns>
		public override string ToString()
		{
			return string.Format(CultureInfo.CurrentCulture, "{0}[{1}]", Package, Version.ToShortDateString());
		}
	}

	///// <summary>
	///// List of package dependencies.
	///// </summary>
	//public class PackageDependencyList
	//{
	//    /// <summary>
	//    /// Constructs an instance of PackageDependencyList.
	//    /// </summary>
	//    /// <param name="dependencies">List of dependencies.</param>
	//    public PackageDependencyList(params PackageDependency[] dependencies)
	//    {
	//        _dependencies = new List<PackageDependency>(dependencies);
	//        _or = false;
	//    }
	//    /// <summary>
	//    /// Constructs an instance of PackageDependencyList.
	//    /// </summary>
	//    /// <param name="dependencies">List of dependencies.</param>
	//    /// <param name="or"></param>
	//    public PackageDependencyList(bool or, params PackageDependency[] dependencies)
	//    {
	//        _dependencies = new List<PackageDependency>(dependencies);
	//        _or = or;
	//    }

	//    List<PackageDependency> _dependencies;
	//    bool _or;

	//    public IList<PackageDependency> Dependencies
	//    {
	//        get { return _dependencies; }
	//    }
	//    public bool Or
	//    {
	//        get { return _or; }
	//    }

	//    public override string ToString()
	//    {
	//        if (_dependencies.Count == 0) return "";
	//        if (_dependencies.Count == 1) return Dependencies[0].ToString();
	//        var sb = new StringBuilder();
	//        if (Or) sb.Append("or ");

	//        foreach (var dependency in _dependencies)
	//            sb.AppendFormat("{0} ", dependency);

	//        return sb.ToString().TrimEnd(' ');
	//    }

	//    public static PackageDependencyList Parse(string dependancies)
	//    {
	//        dependancies = dependancies.Trim();
	//        bool or = false;
	//        if (dependancies.StartsWith("or "))
	//        {
	//            or = true;
	//            dependancies = dependancies.Substring(3).TrimStart(' ');
	//        }
	//        var deps = new List<PackageDependency>();
	//        while (dependancies.Length > 0)
	//        {
	//            deps.Add(PackageDependency.Parse(ref dependancies));	
	//        }

	//        return new PackageDependencyList(or, deps.ToArray());
	//    }
	//}
}
